<template>
    <div class="background">
        <div class="login-container">
            <h2>重置密码</h2>
            <input type="text" v-model="username" placeholder="用户名" />
            <input type="password" v-model="oldpassword" placeholder="请输入旧密码" />
            <input type="password" v-model="newpassword" placeholder="请输入新密码" />
            <button @click="resetPassword">重置密码</button>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                username: "",
                oldpassword: "",
                newpassword: ""
            }
        },
        methods: {
            resetPassword() {
                const token = localStorage.getItem('token'); // 假设 token 存储在 localStorage 中
                this.$axios.put("http://localhost:8080/api/user/resetPassword", {
                    username: this.username,
                    oldpassword: this.oldpassword,
                    newpassword: this.newpassword
                }, {
                    headers: {
                        'x-access-token': token
                    }
                })
                .then(res => {
                    console.log(res.data);
                })
                .catch(error => {
                    if (error.response && error.response.data.code === 7) {
                        console.error("刷新令牌已过期或无效，请重新登录。");
                        // 这里可以引导用户重新登录或刷新令牌
                        this.$router.push('/login');
                    } else {
                        console.error("请求失败:", error);
                    }
                });
            }
        }
    }
</script>